<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <script type="text/javascript">
            function check_album_name_exist()
            {
                document.getElementById("lblMessage").innerHTML = "* Check";
            }
            var xmlHttp;
            function CreateXMLHttpRequest()
            {
                if(window.XMLHttpRequest)
                {
                    return new XMLHttpRequest();
                }
                else if(window.ActiveXObject)
                {
                    return new ActiceXObject("Microsoft.XMLHTTP");
                }
            }     
            function create_album()
            {
                var album = document.getElementById(txtAlbumName).value;
                var avatar = document.getElementById(fAvatar).value;
               
                xmlHttp = CreateXMLHttpRequest();
                var serverURL = "create_album.php?album_name=" + album + "&avatar="+avatar+  "&t=" + (new Date()).getTime();
                xmlHttp.open("GET", serverURL, true);
                xmlHttp.onreadystatechange = function()
                {
                    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
                    {
                        var result = xmlHttp.responseText;			
                        document.getElementById("lblMessage").innerHTML = result;										
                    }
                };
                xmlHttp.send(null); 
            }
        </script>
        <?php
        require 'connect.inc';
        require 'session.inc';
        $res = "";
        $state = 0;
        $result;
        
        if ($_SESSION["isLogin"] == 0)
            header("Location: login.php");
        else {
            if (isset($_GET["btnCreate"])) {
                $name = $_REQUEST["txtAlbumName"];
                $puclic = ($_REQUEST["chkPublic"]);
                $idUser = $_SESSION["iduser"];
                $fileName = "";
                if (count($_FILES) > 0) {
                    $file = $_FILES['fAvatar'];
                    if (!in_array($file['type'], $types)) {
                        return;
                    }
                    if ($file['error'] == 0) {
                        $avatar = $file['name'];
                    }
                }
                $strSQl = "INSERT INTO `album` (`album_name`, `owner`, `date_created`, `date_modified`, `public`)
                    VALUES ($name, $idUser, now(), now(), $puclic, $avatar)";
                $result = mysql_query($strSQL);
                $idAlbum = mysql_insert_id();
                if($result)
                    //Create user's folder and upload file
                    if (mkdir("upload/$idUser", 0777)) {
                        if (mkdir("upload/$idUser/avatar", 0777)) {
                            if ($avatar != "")
                            {
                                $strSQl = "INSERT INTO `photo` (`album_id`, `photo_name`, `date_created`, `date_modified`, `image`)
                                            VALUES ($idAlbum, '$avatar', now(), now(), '$avatar')";
                                $result = mysql_query($strSQL);
                                if($result)
                                    move_uploaded_file($file['tmp_name'], "upload/$idUser/$idAlbum/avatar/$avatar");
                            }
                        }
                    }
            }
        }
        $res .="<table width='100%' border='1' cellspacing='0'>
            <tbody>
                <tr></tr>
                <tr>
                    <td>
                        <img src='logo.jpg'>
                    </td>
                    <td>
                        <h1>Photo Album Website</h1>
                    </td>
                </tr>
                <tr>
                    <td width='20%' valign='top'>
                        <a href='index.php'>Home</a>
                        <br>
                        <a href='my_albums.php'>My Albums</a>
                        <br>
                        <a href='users.php'>Other persons</a>
                    </td>
                    <td>
                    <center>
                        <h2>Create album</h2>
                    </center>
                    <form onsubmit='return check_validationlogin();' action='create_album.php' method = 'GET' name='form1'>
                    <table>
                        <tbody>
                            <tr>
                                <td>

                                </td>
                            </tr>
                            <tr>
                                <td>Album name: </td>
                                <td>
                                    <input type='text' value='' name='txtAlbumName'>
                                </td>
                            </tr>
                            <tr>
                                <td>Avatar: </td>
                                <td>
                                    <input type='file' value='' name='fAvatar'>
                                </td>
                            </tr>
                            <tr>
                            <td>Public: </td>
                                <td>
                                    <input type='checkbox' name='chkPublic'>
                                    
                                </td>
                            </tr>
                            <tr>
                            <td></td>
                                <td>
                                    <input type='submit' value='Create' name='btnCreate'>
                                    <input type='reset' name='btnCancel' value='Cancel'>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form></td>
                <td width='20%' valign='top'>
                    Wellcom <a href='#'>" . $_SESSION["username"] . "</a>!
                |
                <a href='logout.php'>Logout</a>
                </td>
        </tr>
    </tbody>
    </table>";
        echo $res;
        ?>
    </body>

</html>
